// Playground - noun: a place where people can play

import Cocoa



var str = "Hello, playground"


let color = NSColor.blackColor();

for i in 0..100 {
	i*5
}

func exchange<T>(data:T[],i:Int, j:Int)
{
	let T = data[i]
	data[i] = data[j]
	data[j] = T
}


let data = [134,323,232,234]

exchange(data, 0, 2)


data

//exchange(data, 0, 5)

//data //超界 data 变为空, 不再继续执行code


exchange(data, 0, 1)
data


let label = "the width is";
let width = 94;
let widthlable = label + String(width);//去掉String会报错,无法隐式转换,因为不知道要转换那个


let apples = 3;
let appleSummary = "I have \(apples) apples"

var objects = ["1","2","3",5,8.0];
objects[1]
objects[4]
//objects[0] = "wall" //无法赋值,数组里包含不同的种类
objects[0]

var obj2 = ["1","1","1","1","1","1"];
obj2[2] = "2"
obj2


var occupations =
[
    "Malcolm": "Captain",
    "Kaylee": "Mechanic",
]
occupations[ "Jayne"] = "Public Relations" //添加健值对
occupations[ "Malcolm"] = "Sales"
occupations


var myString:String? = "myString"
myString = nil //注释查看效果

if let name = myString {
    println ("hello swift")
    
    name //name的作用域在if括号内
}
else{
    //println(name) 报错
}

//name name的作用域在括号内



//switch case

let vegetable = "red pepper"

switch vegetable {
case "celery":
    let vegetableComment = "Add some raisins and make ants on a log."
    
case "cucumber", "watercress":
    let vegetableComment = "That would make a good tea sandwich."
    
case let x where x.hasSuffix("pepper"):
    let vegetableComment = "Is it a spicy \(x)?"
    
default:
    let vegetableComment = "Everything tastes good in soup."
}




//function and closure

func greet(name: String, day: String) -> String
{
    return "Hello \(name), today is \(day)."
}

greet(" Bob", "Tue sday ")



func sumOf(numbers:Int...)->Int
{
    var sum = 0
    
    for number in numbers {
        sum += number
    }
    
    return sum
}

sumOf()
sumOf(2,3,5)
//sumOf(3.9,2.3) error: wrong type



var numbers = [20, 19, 7, 12]

let maped = numbers.map({ number in 3 * number })
maped
numbers

numbers.sort({$0>$1})
numbers

let print = {
    (number: Int) -> Void in
    
    println(number)
}


//the same expression
var strValue: Optional<String>
var strValue2:String?














